package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.DocenteDAORemote;
import proyecto.EntityBeans.Docente;

@Stateless
@Remote(DocenteBLRemote.class)
public class DocenteBL implements DocenteBLRemote {

	DocenteDAORemote docenteDAORemote;
	
	public int save(Docente docente) {	
		
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return docenteDAORemote.save(docente); //alta de docente		
		
	}
		
	public Docente findById(int id) {
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return docenteDAORemote.findById(id);
		
	}
	
		
	public void merge(Docente docente) {
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
			docenteDAORemote.merge(docente);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
		
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return docenteDAORemote.findAll();
	}
	
	public List findAllNoBaja() {		
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return docenteDAORemote.findAllNoBaja();
	}
	
	
	public List findAllBaja() {		
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return docenteDAORemote.findAllBaja();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return docenteDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idDocente){	
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		docenteDAORemote.remove(idDocente);
	}
	
	public void refresh(Docente docente){
		try
		{
			Context context = new InitialContext();
			docenteDAORemote = (DocenteDAORemote) context.lookup("DocenteDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		docenteDAORemote.refresh(docente);
	}
}
